#!/bin/bash
source /etc/kolla/admin-openrc.sh
# 强制设置UTF-8编码环境
export LANG=zh_CN.UTF-8
export LC_ALL=zh_CN.UTF-8

# 获取OpenStack集群Web IP（从OS_AUTH_URL提取）
OPENSTACK_IP=$(echo $OS_AUTH_URL | awk -F[/:] '{print $4}')

HTML_FILE="openstack-安全组入口统计-$(date +%Y%m%d)-${OPENSTACK_IP}.html"

# HTML头部（纯中文）
cat > "$HTML_FILE" <<'EOF'
<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <title>OpenStack安全组入口规则报告</title>
    <style>
        body { font-family: "Microsoft YaHei", Arial, sans-serif; margin: 20px; }
        h1 { color: #2c3e50; text-align: center; }
        .project { margin-bottom: 30px; border-left: 4px solid #3498db; padding-left: 15px; }
        .project-header { color: #e74c3c; font-size: 20px; margin-top: 25px; }
        .sg { background: #f8f9fa; border: 1px solid #ddd; padding: 15px; margin-bottom: 20px; border-radius: 5px; }
        .sg-name { font-size: 18px; color: #3498db; margin-top: 0; border-bottom: 1px solid #eee; padding-bottom: 5px; }
        table { width: 100%; border-collapse: collapse; margin-top: 10px; }
        th { background: #3498db; color: white; text-align: left; padding: 10px; }
        td { padding: 8px; border-bottom: 1px solid #ddd; }
        tr:nth-child(even) { background: #f2f2f2; }
        .timestamp { color: #7f8c8d; text-align: center; font-size: 14px; }
        .no-rules { color: #999; text-align: center; font-style: italic; padding: 10px; }
        .no-sg { color: #95a5a6; font-style: italic; margin: 10px 0; }
    </style>
</head>
<body>
    <h1>OpenStack安全组入口规则报告</h1>
    <p class="timestamp">生成时间:
EOF

# 添加中文格式的时间
date "+%Y年%m月%d日 %H时%M分%S秒" >> "$HTML_FILE"

cat >> "$HTML_FILE" <<'EOF'
    </p>
EOF

# 处理规则函数（中文字段）
process_rules() {
    sg_id=$1
    rule_count=$(openstack security group rule list --ingress "$sg_id" -f value | wc -l)

    if [ "$rule_count" -eq 0 ]; then
        echo '<tr><td colspan="6" class="no-rules">无入口规则</td></tr>' >> "$HTML_FILE"
    else
        openstack security group rule list --ingress "$sg_id" -f value \
            -c "IP Protocol" -c Ethertype -c "IP Range" -c "Port Range" -c "Remote Security Group" -c Description | \
            while read -r protocol ethertype ip_range port_range remote_sg description; do
                # 处理协议
                protocol=${protocol:-全部}

                # 处理端口范围
                if [ -z "$port_range" ] || [ "$port_range" = "None" ]; then
                    port_range="任何"
                fi

                # 处理远端IP
                if [ -n "$remote_sg" ] && [ "$remote_sg" != "None" ]; then
                    remote_target="安全组: $remote_sg"
                elif [ -n "$ip_range" ]; then
                    remote_target="$ip_range"
                else
                    remote_target="任何"
                fi

                printf "<tr>"
                printf "<td>入站</td>"
                printf "<td>%s</td>" "$protocol"
                printf "<td>%s</td>" "$port_range"
                printf "<td>%s</td>" "$ethertype"
                printf "<td>%s</td>" "$remote_target"
                printf "<td>%s</td>" "${description:-无}"
                printf "</tr>\n"
            done >> "$HTML_FILE"
    fi
}

# 获取所有项目
openstack project list -f value -c ID -c Name | while read -r project_id project_name; do
    # 写入项目信息
    cat >> "$HTML_FILE" <<EOF
    <div class="project">
        <h2 class="project-header">项目: ${project_name} (ID: ${project_id})</h2>
EOF

    # 获取项目下的安全组
    sg_count=$(openstack security group list --project $project_id -f value | wc -l)

    if [ "$sg_count" -eq 0 ]; then
        echo '<p class="no-sg">该项目下无安全组</p>' >> "$HTML_FILE"
    else
        # 遍历项目下的安全组
        openstack security group list --project $project_id -f value -c ID -c Name | while read -r sg_id sg_name; do
            cat >> "$HTML_FILE" <<EOF
        <div class="sg">
            <h3 class="sg-name">安全组: ${sg_name} (ID: ${sg_id})</h3>
            <table>
                <tr>
                    <th>方向</th>
                    <th>协议</th>
                    <th>端口范围</th>
                    <th>IP类型</th>
                    <th>远端IP</th>
                    <th>描述</th>
                </tr>
EOF

            process_rules "$sg_id"

            echo "</table></div>" >> "$HTML_FILE"
        done
    fi

    echo "</div>" >> "$HTML_FILE"  # 关闭项目div
done

# HTML尾部
cat >> "$HTML_FILE" <<'EOF'
</body>
</html>
EOF

echo "中文报告已生成: $HTML_FILE"
echo "下载命令: sz $HTML_FILE"